import { Get, Inject, Provide, Query, ALL } from '@midwayjs/decorator';
import { BaseController, CoolController, RESCODE } from 'midwayjs-cool-core';

import { HistoryPageDto } from '../dto/history';
import { CustomerHistoryEntity } from '../entity/history';
import { CustomerHistoryService } from '../service/history';

/**
 * 需要自定义 page 接口：拼接 product 信息
 */
@Provide()
@CoolController({
  prefix: '/customer/favorite',
  api: ['add', 'delete', 'update'],
  entity: CustomerHistoryEntity,
  service: CustomerHistoryService,
})
export class CustomerFavoriteController extends BaseController {
  @Inject()
  customerHistoryService: CustomerHistoryService;

  @Get('/info')
  async info(@Query() id: number) {
    try {
      const data = await this.customerHistoryService.info(id);
      return this.ok(data);
    } catch (err) {
      return this.fail(err.message, RESCODE.COMMFAIL);
    }
  }

  @Get('/page')
  async page(@Query(ALL) pageOp: HistoryPageDto) {
    try {
      const { count, data } = await this.customerHistoryService.page(pageOp);
      return this.ok({
        favorites: data,
        hasMore: pageOp.start + pageOp.limit < count,
        count,
      });
    } catch (err) {
      return this.fail(err.message, RESCODE.COMMFAIL);
    }
  }
}
